package com.ffcs.crmd.tsp.core.netty;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 
 * 功能说明:保证信号量多线程下只释放一次
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class SemaphoreReleaseOnlyOnce {
    /**
     * 
     */
    private AtomicBoolean released = new AtomicBoolean(false);
    
    /**
     * 
     */
    private Semaphore     semaphore;
    
    public SemaphoreReleaseOnlyOnce(Semaphore semaphore) {
        this.semaphore = semaphore;
    }
    
    /**
     * 
     * 功能说明:释放信号量
     *
     */
    public void release() {
        if (this.semaphore != null) {
            if (this.released.compareAndSet(false, true)) {
                this.semaphore.release();
            }
        }
    }
    
}
